<? /* license

BilugCMS (http://www.bilug.it) - Content Management System for dynamic web sites
Copyright (C) 2005-2008  Federico Villa and Alessio Loro Piana

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 2 of the License.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.

For reference, contact bilugcms@vilnet.it


license */ ?>
<?
require_once("funzioni.php");
$nav = $_POST['Nav'];
$start = $_POST['start'];
$argo = $_GET['argo'];
$d = $_GET['d'];
if (!isset($argo)) $argo = $_POST['argo'];
if (!isset($d)) $d = $_POST['d'];
if (!isset($argo)) $argo = _DEFARGGAL;
if (!isset($d)) $d = _DEFGAL;
$argo = apici($argo);
$d = apici($d);

echo "<h3 class=\"Cap\">Galleria: ".str_replace("_"," ",$argo)." &raquo; ";
echo str_replace("_"," ",substr($d,strpos($d,"_")+1));
echo "</h3>";	
//Variabili
$directory ="../gals/".$argo.'/'.$d.'/';
$filtro = array("image/jpg","image/jpeg","image/gif","image/png");
$spazio = _MAX_SPAZIO;
$col = floor($spazio/_MAX_LARG_FOTO);
$page = _MAX_FOTO;
$max = 0;
//funzione navigazione
function Nav($max,$pos,$next,$option)
{
	//Creo la form per navigare
 	echo "<form method=\"post\" action=\"index.php?pag=../utility/galleria.php\" name=\"log\">\n
 	<h3>" . ($pos) . " - " . $max . "</h3>\n";
 	
 	foreach ($option as $key =>$value) 	
 		echo"<input type=\"hidden\" name=\"$key\" value=\"$value\"/>\n";
 		 
 	if ($pos>0) 
 	echo "<input type=\"submit\" class=\"gal\" value=\"Primo\" name=\"Nav\" />\n
 			<input type=\"submit\" class=\"gal\" value=\"Precedenti\" name=\"Nav\" />\n";
 	
 	if ($next<$max) 
 		echo "<input type=\"submit\" class=\"gal\" value=\"Successivi\" name=\"Nav\"/>\n
 				<input type=\"submit\" class=\"gal\" value=\"Ultimo\" name=\"Nav\" />";
 				
 	echo "</form>\n";
}
//crezione array per la galleria
if ($handle = @opendir($directory))
{
	//setto gli array delle immagini		
	$immagine = array();
	
	while (false !== ($file = readdir($handle)))
	{
		$dati= getimagesize($directory . $file);		
		if (in_array($dati['mime'],$filtro))   	
   		$immagine[]= $file;
   }
   closedir($handle);
   
   if (isset($immagine) and count($immagine)>0)
   {
   	$img = array();
   	natcasesort($immagine);
   	$max = count($immagine);   
   	foreach ($immagine as $key => $value)   
			$img[] = $value;
		
		if(!is_numeric($start)) $start=0;
		if($nav== 'Successivi') $start+= $page;
		if($nav== 'Precedenti') $start-= $page;
		if($nav== 'Primo') $start= 0;
		if($nav== 'Ultimo') $start= $max-$page;
		if ($start<0) $start = 0;

		echo "<div class=\"contenitore\">";
		if ($max>$page)          
	 		Nav($max,$start+1,$start+$page,array("d" =>$d, "argo" =>$argo,"start" => $start));	
		
		//fine Nav  Tabella
		$inc = 1;
		$n = $start;
		($start+$page<=$max)?$fine =$start+$page : $fine=$max;
		
		for ($n = $start;$n<$fine;$n++)
		{
			if ($inc>=$col)
			{
				$inc=1;
  				echo "<div class=\"azzerafloat\"></div>";
  			}
  			// verifica se c'e' descrizione
   		$str="SELECT descrizione FROM galleria WHERE immagine='$d/$img[$n]' ";
  			// facciamo una query per caricare tutti i dati della tabella
  			$risultato=mysql_query($str);  		
  			if (mysql_num_rows($risultato)>0)
  				$title= str_replace("\n","|",mysql_fetch_row($risultato));
  			else
  				$title="";
  			$nameimm = str_replace("_"," ",$img[$n]);
  	
   		echo "<div class=\"galleria\">\n<div class=\"galleria1\">\n<a";   	
   		if ($title[0]!= "") 
   			echo " title=\"".
   			wordwrap(substr($nameimm, 0, strripos($nameimm, '.')),22,"\n",1).
   			"|$title[0]\"";
   			   	
   		echo " href=\"".$directory.$img[$n]."\">   				
   			<img valign=\"middle\" 
   			src=\"../utility/thump.php?w=".(_MAX_LARG_FOTO-10)."&amp;h=".(_MAX_LARG_FOTO-10)."&amp;file=".$directory.$img[$n]."\"";
   			 
	   	if ($title[0]!= "") 
	   		echo " alt=\"".wordwrap(str_replace("|","<br/>",$title[0]),70,"<br/>",1)."\"/>";
   		else echo "alt=\"".str_replace("_"," ",$img[$n])."\"/>";
   			
   		echo "</a><br/><span class=\"galnome\" >  </div>".
   			wordwrap(substr($nameimm, 0, strripos($nameimm, '.')),22,"\n",1)."</span>
   			</div>" ;
		}
		echo "<div class=\"azzerafloat\"></div>";
		if ($max>$page)
			Nav($max,$start+1,$start+$page,array("d" =>$d, "argo" =>$argo,"start" => $start));          
	 
	
	echo "</div>";
	}
	else
	{
		echo "<div class=\"contenitore\">
			<h3>Nessuna Immagine in questa Galleria</h3>
			</div>";
	}
}
else 
{
	echo "<div class=\"contenitore\">
			<h3>Galleria Inesistente</h3>
			</div>";	
}
ob_end_flush() ;
?>
